package com.wei.tang.ui.me;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.base.BaseActivity;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.UserDao;
import com.wei.tang.util.Tlog;

import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 用户登录界面
 *
 */
public class RegistLoginActivity extends BaseActivity {

	private RegistLoginActivity mySelf = this;

	@InjectView(R.id.et_username)
	EditText mEtUserName;

	@InjectView(R.id.et_password)
	EditText mEtPassword;

	private String mUserName = "";
	private String mPassword = "";

	@Override
	public void initView() {
	}

	@Override
	public int getLayoutId() {
		return R.layout.register_login_activity;
	}

	@Override
	public void initData() {
		mEtUserName.setText(AppContext.getInstance().getProperty("user.phone"));
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.app_name;
	}

	@Override
	@OnClick({ R.id.btn_login, R.id.btn_register, R.id.btn_forget })
	public void onClick(View v) {
		Intent intent = null;
		int id = v.getId();
		switch (id) {
		case R.id.btn_login:
			handleLogin();
			break;
		case R.id.btn_register:
			intent = new Intent(mySelf, RegistInformationActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_forget:
			intent = new Intent(mySelf, RegistForgetActivity.class);
			startActivity(intent);
			break;
		}
	}

	private void handleLogin() {

		if (prepareForLogin()) {
			return;
		}

		mUserName = mEtUserName.getText().toString();
		mPassword = mEtPassword.getText().toString();

		showWaitDialog(R.string.loading);

		LoginTask loginTask = new LoginTask();
		loginTask.execute(0);

	}

	class LoginTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			UserDao userDao = new UserDao(mySelf);
			UserBean userBean = userDao.findUserByPhonePass(mUserName, mPassword);
			if (userBean == null) {
				msg = "账号和密码错误，请重新登录！";
				code = AppConest.WS_FALL;
			} else {
				try {
					AppContext.getInstance().userLogin(userBean);
				} catch (Exception e) {
					msg = e.getMessage();
					code = AppConest.WS_FALL;
				}
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			hideWaitDialog();

			if (code == AppConest.WS_FALL) {
				Tlog.e(msg);
				AppContext.showToast(msg);
				return;
			}

			if (AppConest.ISLOGIN) {
				AppContext.showToast("登录成功");
				handleLoginSuccess();
				mySelf.finish();
			} else {
				AppContext.showToast("登录失败");
			}
		}

	}

	private void handleLoginSuccess() {
		AppContext.getInstance().setProperty("user.session", AppContext.USERBEAN.getUser_session());
	}

	/**
	 * 对输入输入的数据进行检查
	 */
	private boolean prepareForLogin() {
		if (mEtUserName.length() == 0) {
			mEtUserName.setError("请输入手机号码");
			mEtUserName.requestFocus();
			return true;
		}

		if (mEtPassword.length() == 0) {
			mEtPassword.setError("请输入密码");
			mEtPassword.requestFocus();
			return true;
		}
		return false;
	}
}
